/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;


import java.util.Iterator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Event;
import org.eclipse.uml2.uml.Trigger;

import com.cea.papyrus.core.PapyrusPlugin;

/**
 * Dialog to select an Event to be associated to a Trigger.
 * 
 * @author David Servat
 * @since 5. apr. 2007
 */
public class TriggerEventTreeSelectionDialog extends Dialog{

	/**
	 * The Trigger for which we want to set an associated Event.
	 */
	protected Trigger trigger;

	/**
	 * Associated event.
	 */
	protected Event event;

	/**
	 * The selection tree.
	 */
	protected Tree tree;

	/**
	 * Constructor with parent shell and trigger
	 * 
	 * @param trigger the Trigger
	 * @param parent the Shell
	 */
	public TriggerEventTreeSelectionDialog(Shell parent, Trigger trigger){
		super(parent);
		// add the resize ability to the window
		setShellStyle(SWT.RESIZE | super.getShellStyle());
		this.trigger = trigger;
	}

	/**
	 * Creates the dialog area
	 * 
	 * @param parent composite
	 * 
	 * @return control
	 */
	protected Control createDialogArea(Composite parent){
		Composite composite = (Composite)super.createDialogArea(parent);
		tree = new Tree(composite, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);

		//build tree item for each Event contained in the nearest Package owning the trigger
		Iterator iter = trigger.getNearestPackage().allOwnedElements().iterator();
		while(iter.hasNext()){
			Element currentElement = (Element)iter.next();
			if(currentElement instanceof Event){
				Event currentEvent = (Event)currentElement;
				TreeItem item = new TreeItem(tree, SWT.NONE);
				item.setText(currentEvent.getQualifiedName());
				item.setData(currentEvent);
				item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(currentEvent));
			}
		}

		tree.setLayoutData(new GridData(550, 300));

		tree.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e){
				TreeItem[] selection = tree.getSelection();
				if(selection[0].getData() instanceof Event){
					event = (Event)selection[0].getData();
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		return composite;
	}


	/**
	 * Returns the associated Event.
	 * 
	 * @return event Event
	 */
	public Event getResult(){
		return event;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(trigger.getName()+": Event Selection");
	}

}
